//
//  MKTreeNode.h
//  MKUI
//	简单的树数据结构中的结点
//  Created by sushangjin on 15/6/18.
//  Copyright (c) 2015年 sushangjin. All rights reserved.
//

#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface MKTreeNode : NSObject{
	@protected
	NSMutableArray<__kindof MKTreeNode *> *_childrenNodes;
}
@property(nonatomic,assign) BOOL isLeaf;//是否是叶结点,否则为分支结点,可以含有子结点
@property(nonatomic,readonly,nullable) NSArray<__kindof MKTreeNode *> *childrenNodes;//子结点
@property(nonatomic,weak,nullable) __kindof MKTreeNode *parentNode;//弱引用父结点
@property(nonatomic,strong,nullable) id userInfo;//扩展的数据栏位

@property(nonatomic,readonly) NSInteger level;//层级
@property(nonatomic,readonly) NSArray<__kindof MKTreeNode *> *allNodes;//返回所有的结点(包含自己与后代)

- (void)addChildNode:(MKTreeNode *)node;
- (void)insertChildNode:(MKTreeNode *)node atIndex:(NSInteger)index;
- (void)removeChildNode:(MKTreeNode *)node;
- (void)removeChildNodeAtIndex:(NSInteger)index;

- (nullable __kindof MKTreeNode *)childNodeAtIndex:(NSInteger)index;
- (NSInteger)indexOfChildNode:(MKTreeNode *)node;
/**
 *  查找node在本结点树下的结点路径
 *
 *  @param node 要查找的结点
 *
 *  @return 从self开始到node的结点路径数据
 */
- (nullable NSArray<__kindof MKTreeNode *> *)nodePathOfNode:(MKTreeNode *)node;
- (nullable NSArray<__kindof MKTreeNode *> *)nodePathOfNode:(MKTreeNode *)node passingTest:(BOOL(^ __nullable)(MKTreeNode *n1,MKTreeNode *n2))passingTest;

/**
 *  在指定的根结点列表中,查找node结点的结点路径
 *
 *  @param node      要查找的结点
 *  @param rootNodes 根结点列表
 *
 *  @return 从rootNodes中结点开始到node的结点路径数据
 */
+ (nullable NSArray<__kindof MKTreeNode *> *)nodePathOfNode:(MKTreeNode *)node rootNodes:(NSArray *)rootNodes;
+ (nullable NSArray<__kindof MKTreeNode *> *)nodePathOfNode:(MKTreeNode *)node rootNodes:(NSArray *)rootNodes passingTest:(BOOL(^ __nullable)(MKTreeNode *n1,MKTreeNode *n2))passingTest;
@end
NS_ASSUME_NONNULL_END
